package com.game.basic;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

import com.game.config.Config;
import com.game.state.BasicState;
import com.game.state.BasicLogoState;
import com.game.utils.GamePreference;
import com.game.utils.GameRMS;
import com.game.utils.GameSoundManager;
import com.game.utils.ImageTool;
import com.game.utils.LoadingThread;
import com.game.utils.PhoneInfo;
import com.game.utils.SystemInfoUtil;

/*      Activity生命周期
 * 
			 create
			   |
			 start
			   |
			 resume
			   |
			running
			   |(当另外一个窗口出现)   
			 pause 当activity重新返回到屏幕最上方,调用resume|当内存不足activity会被kill
			   |(当此activity完全不可见)
			  stop 当activity重新返回到屏幕最上方,调用restart,restart调用start|当内存不足activity会被kill
			   |
			 destory*/


public abstract class BasicActivity extends Activity{
	//当前view
	public BasicView gameView;
	//静态实例,整个游戏只有一个
	public static BasicActivity instance;
	//标识activity是否可见
	public  boolean visible=true;
	//logo后的state
	public BasicState logoState;
	public BasicState firstState=null;
	public int logoCount=0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i("infos", "创建Activity 调用onCreate=============================");
		
		instance=this;
		
        //去除标题,去除通知栏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		//强制横屏  在配置文件中设置强制横屏,不在这里设置
//        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        
        //设置调整音量时调整多媒体音
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        
        //获得display信息
        Display display = getWindowManager().getDefaultDisplay();
        
        //初始化手机信息
        PhoneInfo.newInstance(display,getRequestedOrientation()==0?true:false);
        
        //初始化手机信息工具
        SystemInfoUtil.newInstance(this);
        
        //初始化ImageTool
        ImageTool.newInstance(this.getResources());
        
        //初始化数据库
        new GameRMS(this);
        
        //初始化设置
        new GamePreference(this);
        
        //初始化音效管理
        new GameSoundManager(this);
        
        //初始化游戏 
        initGame();
        
        //初始化logo状态 和logo后的第一个状态
        initFirstState();
        
	    //跳转到logo
	    gameView.changeState(logoState);
        
        //显示gameView
	    setContentView(gameView);
	    
	}
	
	/**
	 * 初始化刚进入游戏的第一个状态
	 * @return
	 */
	public BasicState initFirstState(){
		// logo图片数   第一个状态
    	logoState=new BasicLogoState(logoCount,firstState);
    	return logoState;
	}
	
	/**
	 * 初始化一个gameview
	 */
	public BasicView initView(){
        if(gameView==null){
        	gameView = new BasicView(this) {
				@Override
				public void paint(Canvas canvas) {
					
				}
			};
        }
        return gameView;
	}
	
	//初始化游戏  可在此方法中定义 logoCount  定义 firstState 甚至自己的view
	public abstract void initGame();
	
	 /**
     * activity重新回到屏幕最上方*/
    @Override
    protected void onResume() {
    	super.onResume();
    	Log.i("infos", "恢复Activity 调用onResume=============================");
    	visible=true;
    	if(GameSoundManager.instance!=null){
    		GameSoundManager.instance.palyMusic();//播放背景音
    	}
    }
    
	/**
	 * activity暂停,不在屏幕最上方,但仍可见
	 * 暂停绘制
	 * 当activity不处于屏幕最上方 执行此方法*/
    @Override
    protected void onPause() {
    	super.onPause();
    	Log.i("infos", "暂停Activity 调用onPause=============================");
    	if(gameView!=null){
    		gameView.pause=true;//暂停游戏
    	}
    	if(GameSoundManager.instance!=null){
    		GameSoundManager.instance.pauseMusic();//停止播放背景音
    	}
    }
    
    /**
     * activity停止,不在屏幕最上方,完全不可见*/
    @Override
    protected void onStop() {
    	super.onStop();
    	Log.i("infos", "停止Activity 调用onStop=============================");
    	if(gameView!=null){
    		gameView.drawing=false;//停止绘制游戏
    	}
    }
    
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	Log.i("infos", "销毁Activity 调用onDestroy=============================");
    	/*保存有所游戏配置*/
    	if(GamePreference.instance!=null){
    		GamePreference.instance.saveAll(Config.GLOBAL_SETTINGS);
    	}
    }
    
    /**
     * 触摸事件*/
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	/*由于view接收了touchu事件,因此这里不会执行*/
    	Log.i("infos", "触摸Activity");
    	return true;
    }
	
	/**
	 * 重载资源*/
	public void reloadRes(){
		if(gameView!=null){
			gameView.loading=true;
			new LoadingThread(gameView.gameState).start();
		}
	}
	
	public void destoryApp(){
		onDestroy();
		android.os.Process.killProcess(android.os.Process.myPid());
		System.exit(0); 
	}
	
}